package com.p900.website.web;

import com.p900.commons.domain.Platform;
import com.p900.commons.domain.Product;
import com.p900.website.service.AdvertisementService;
import com.p900.website.service.PlatformService;
import com.p900.website.service.ProductService;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2015/5/1.
 */
@Controller
public class IndexController {

    private static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private AdvertisementService advertisementService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private ProductService productService;

    @Value("${application.message:Hello World}")
    private String message = "Hello World";

    @RequestMapping({"", "/index.html"})
    public String index(Model model) {
        // 平台列表
        Page<Platform> platformPage = platformService.findAll(new PageRequest(0, 10));

        // 平台产品
        Map<Long, List<Product>> productsInPlatform = new HashMap<Long, List<Product>>();
        for(Platform platform : platformPage.getContent()){
            productsInPlatform.put(platform.getId(), productService.findByPlatformId(platform.getId(), new PageRequest(0, 2)).getContent());
        }

        //热门商品
        List<Product> hotProducts = productService.findHotProducts(new PageRequest(0, 3)).getContent();
        Map<Long, Platform> productsPlatform = new HashMap<Long, Platform>();
        for(Product product : hotProducts){
            productsPlatform.put(product.getId(), platformService.findPlatform(product.getPlatformId()));
        }

        // 返回页面
        model.addAttribute("advertisements", advertisementService.findByStatus(1));
        model.addAttribute("platforms", platformPage);
        model.addAttribute("productsInPlatform", productsInPlatform);
        model.addAttribute("hotProducts", hotProducts);
        model.addAttribute("productsPlatform", productsPlatform);

        return "site/index";
    }

    @RequestMapping("/welcome")
    public String welcome(Map<String, Object> model) {
        model.put("time", DateTime.now().toDate());
        model.put("message", this.message);
        return "site/welcome";
    }

    @RequestMapping("/errorPage/500")
    public String errorPage500(String msg) throws Exception {
        if(msg == null)
            throw new IllegalArgumentException("The 'name' parameter must not be null or empty");
        return "site/commons/500";
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public void handleBadRequests(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.error("12121212");
        response.sendError(HttpStatus.BAD_REQUEST.value(), "Please try again and with a non empty string as 'name'");
    }
}
